<!DOCTYPE html>
<html>
<head>
    <title>添加优惠卷</title>
    {% include 'admin/common/header.html' %}
</head>
{#改变下拉框时的字体颜色#}
<style>
    .layui-form-select dl dd.layui-this{
        color: white;
    }
</style>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">

                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                               class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">有效期</label>
                    <div class="layui-inline" id="test6">
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" name="start" id="test-startDate-1" class="layui-input"
                                   placeholder="开始日期">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" name="end" id="test-endDate-1" class="layui-input"
                                   placeholder="结束日期">
                        </div>
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">优惠卷类型</label>
                    <div class="layui-input-block">

                        <select name="classify" lay-verify="title">
                            {% for cla in coupons %}
                                <option value="{{ cla.id }}">{{ cla.name}}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">满足多少钱可用</label>
                    <div class="layui-input-block">
                        <input type="number" value="" name="minmoney" lay-verify="required"
                               autocomplete="off" placeholder="请输入密码"
                               class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">减免多少钱</label>
                    <div class="layui-input-block">
                        <input type="number" value="" name="submoney" lay-verify="required"
                               autocomplete="off" placeholder="请输入密码"
                               class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">优惠卷数量</label>
                    <div class="layui-input-block">
                        <input type="number" value="" name="number" lay-verify="required"
                               autocomplete="off" placeholder="请输入密码"
                               class="layui-input">
                    </div>
                </div>


                {#                <div class="layui-form-item">#}
                {#                    <label class="layui-form-label">角色</label>#}
                {#                    <div class="layui-input-block">#}
                {##}
                {#                        {% for role in roles %}#}
                {#                            <input#}
                {#                                    value="{{ role.id }}" title="{{ role.name }}" type="checkbox"#}
                {#                                    name="roleIds" lay-skin="primary">#}
                {#                        {% endfor %}#}
                {##}
                {#                    </div>#}
                {#                </div>#}
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'admin/common/footer.html' %}
<script>
    layui.use(['laydate', 'upload', 'form', 'jquery'], function () {
        let form = layui.form
        let $ = layui.jquery
        let upload = layui.upload
        let imgpath = ''

        let laydate = layui.laydate;

        //日期范围
        laydate.render({
            elem: '#test6'
            //设置开始日期、日期日期的 input 选择器
            //数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
            , range: ['#test-startDate-1', '#test-endDate-1']
        });



        //拖拽上传
        upload.render({
            elem: '#uptest'
            , url: '{{ url_for('wxuser.upload_api') }}' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            {#,url: 'https://httpbin.org/post'#}
            , exts: 'jpg|png|gif|bmp|jpeg'
            , done: function (res) {
                layer.msg('上传成功', {icon: 1});
                {#console.log(res.img)#}
                let html = " <img src='" + res.data.src + "' alt='上传成功后渲染' style='max-width: 103px;position: relative;margin-left: 190px;margin-top: -143px;display: inline-block'>"
                layui.$('#uploadDemoView').append(html)
                {#find('img').attr('src', res.data.src);#}
                imgpath = res.data.src
                console.log(imgpath)
                {#layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.files.file);#}
                console.log(res)
            }
        });


        form.on('submit(user-save)', function (data) {
            console.log(data.field)
            {#let roleIds = ''#}
            {#$('input[type=checkbox]:checked').each(function () {#}
            {#    roleIds += $(this).val() + ','#}
            {#//})#}
            {#roleIds = roleIds.substr(0, roleIds.length - 1)#}
            {#data.field.roleIds = roleIds#}
            data.field.file = imgpath
            $.ajax({
                url: '/coupon/addcoupon',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
                            parent.layui.table.reload('user-table')
                        })
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000})
                    }
                }
            })
            return false
        })
    })
</script>
<script>
</script>
</body>
</html>